<template>
  <div class="f-menu" :style="{ width: $store.state.asideWidth }">
    <el-menu
      class="border-0"
      @select="handleSelect"
      :collapse="isCollapse"
      collapse-transition:false
      :default-active="defaultActive"
      unique-opened
      background-color="#F0E2BD"
    >
      <template v-for="(item, index) in asideMenus" :key="index">
        <!-- 一级菜单 -->
        <el-sub-menu
          v-if="item.child && item.child.length > 0"
          :index="item.name"
        >
          <!-- 一级菜单图标 -->
          <template #title>
            <el-icon>
              <component :is="item.icon"></component>
            </el-icon>
            <span>{{ item.name }}</span>
          </template>
          <!-- 二级菜单 -->
          <el-menu-item
            v-for="(item2, index2) in item.child"
            :key="index2"
            :index="item2.frontpath"
          >
            <el-icon>
              <component :is="item2.icon"></component>
            </el-icon>
            <span>{{ item2.name }}</span>
          </el-menu-item>
        </el-sub-menu>

        <el-menu-item v-else :index="item.frontpath">
          <el-icon>
            <component :is="item.icon"></component>
          </el-icon>
          <span>{{ item.name }}</span>
        </el-menu-item>
      </template>
    </el-menu>
  </div>
</template>
<script setup>
import { useRouter, useRoute, onBeforeRouteUpdate } from 'vue-router'
import { useStore } from 'vuex'
import { computed, ref } from 'vue'
import { addMenus } from '@/router/addMenus'

const router = useRouter()
const store = useStore()
const route = useRoute()

// 页面加载时默认激活菜单
const defaultActive = ref(route.path)
// 监听路由变化
onBeforeRouteUpdate((to, from) => {
  defaultActive.value = to.path
})
// console.log(route.path)
// 菜单数据
// const asideMenus = [
//   {
//     name: '后台面板',
//     icon: 'help',
//     child: [
//       {
//         name: '主控台',
//         icon: 'home-filled',
//         frontpath: '/home'
//       }
//     ]
//   },
//   {
//     name: '商城管理',
//     icon: 'shopping-bag',
//     child: [
//       {
//         name: '商品管理',
//         icon: 'shopping-cart-full',
//         frontpath: '/goods/list'
//       }
//     ]
//   }
// ]
// const asideMenus = computed(() => store.state.menus)
let menus = computed(() => store.state.menus)
// 自己添加的菜单-->已经封装到了router/addMenus当中
// let addMenus = [
//   {
//     name: '练习模块',
//     icon: 'Collection',
//     child: [
//       {
//         name: '拖拽库',
//         icon: 'Grape',
//         frontpath: '/antv/list'
//       }
//     ]
//   }
// ]
const asideMenus = computed(() => [...menus.value, ...addMenus])

// 实现菜单跳转
const handleSelect = e => {
  router.push(e)
}
// 实现菜单折叠
const isCollapse = computed(() => !(store.state.asideWidth == '250px'))
</script>

<style lang="scss" scoped>
.f-menu {
  // width: 250px;
  transition: all 0.3s;
  top: 60px;
  bottom: 0;
  left: 0;
  overflow-y: auto;
  overflow-x: hidden;
  @apply shadow-md fixed bg-light-50 bg-[#F0E2BD];
}
.f-menu::-webkit-scrollbar {
  width: 0px;
}
</style>
